package com.easypay;

import java.security.MessageDigest;

public class PayCode {
    public static void main(String[] args) {
        //        Sign Before MD5:body=JSAPI TEST&channelid=
//
//            D01X66666666741&customerip=127.0.0.1&merid=
//            531000012972133&notifyurl=http://lianmeng.apnpay.com/receiverapi/notify/preNotify&openid=admin4&opt=
        // upPreOrder&returnurl=http://lianmeng.apnpay.com/receiverapi/notify/preNotify&
        // termid=
        // W1489460&tradeamt=1&tradetrace=7d762db2b1004aada9889e6d45e6ff19&tradetype=JSAPI&version=2&key=o8daidall7dl35lad8dlavd4d0ddjjrr

        //JSONObject jsonObject = JSON.parseObject("{md=531000009435881,, cd=0e7491aa0a8954d4, od=SyG/RDK/iqWW3fyDGe/ebFmpG9ecv2i5r0Juyt9ux7CGsqsuFIfnDqsAuGDhsfLx, amt=4698, mode=up, term=SHW0840485}");
//        String cd = "0e7491aa0a8954d4";
//        String key = "qwds732r5gf3ff8c";
//        //md + od + term
//        String src = "531000009435881SyG/RDK/iqWW3fyDGe/ebFmpG9ecv2i5r0Juyt9ux7CGsqsuFIfnDqsAuGDhsfLxSHW0840485" + key;
//        System.out.println(checkMD5(src, cd, key));

        // {md=531000009454251,, cd=9b51b10f5ebd2251, od=58971069, amt=60, mode=up, term=SHW0842616}
        // {md=890000000168225,, cd=e380f3aa59d71df7, od=tPvsEwIvRkjLUvwPjn0eiAa3vX88dRNcaFgrpN0GADvCLdRQXTFePtspA4%2BJniBB, amt=1314, mode=up, term=SHW0830228}

        // {md=890000000168225,, cd=16b8874e2bdb33e9, od=v0jZStLpYSWUCAOVtDP8aAVP8%2Bhsx1wtePYdA%2FhjUopsz8%2BL1%2Bhmz%2FrqxiXb2fGJ, amt=0.01, mode=up, term=SHW0830228}
        //https://mobile.eycard.cn/TM0000000000037.html?mode=up&md=890000000168225,&od=wQT0QUXvNTQmqqdUakFWIWS1Z5blQhOqCzlpnMtJk1s2kk3XzXSFuM+hvC7TCVC1&term=SHW0830228&mdesc=%E5%95%86%E6%88%B7_%E6%B2%83%E6%99%93%E8%8E%B9&dc=W0830228&v=e594dr&cd=3df1b36774463842


//        String cd = "3df1b36774463842";
//        String key = "qwds732r5gf3ff8c";
//        //md + od + term
//        String md = "890000000168225";
//        String od = "wQT0QUXvNTQmqqdUakFWIWS1Z5blQhOqCzlpnMtJk1s2kk3XzXSFuM+hvC7TCVC1";
//        String term = "SHW0830228";
//        String src = md + od + term;
//        System.out.println(checkMD5(src, cd, key));

        // System.out.println(URLDecoder.decode("v0jZStLpYSWUCAOVtDP8aAVP8%2Bhsx1wtePYdA%2FhjUopsz8%2BL1%2Bhmz%2FrqxiXb2fGJ", "UTF-8"));


//{md=890000000168225,, cd=451e3edb2a1ea540, od=2FBtPlUDikshuAZqoeaBws%2B8WfjTnAywL5rIfixFqO6Q90%2BSxeA%2B2haYqm7OmOkJ, amt=0.03, mode=up, term=SHW0830228}
        // {md=531000012972421,, cd=8d6ee5d522e73f43, od=oIcV7wwvKsNAxdnTNhg2ylI4Oibo, amt=1, mode=wx, term=SHW1489541}

        // md=890000000168225,, cd=3df1b36774463842, od=wQT0QUXvNTQmqqdUakFWIWS1Z5blQhOqCzlpnMtJk1s2kk3XzXSFuM+hvC7TCVC1, amt=0.02, mode=up, term=SHW0830228
        // {md=890000000168225,, cd=3df1b36774463842, od=wQT0QUXvNTQmqqdUakFWIWS1Z5blQhOqCzlpnMtJk1s2kk3XzXSFuM+hvC7TCVC1, amt=0.02, mode=up, term=SHW0830228}

        // {md=531000009467677,, cd=cbeeccfaaf2ed67a, od=2088231760502253, amt=0.01, mode=al, term=SHW0843820}
//        String cd = "cbeeccfaaf2ed67a";
//        String key = "qwds732r5gf3ff8c";
//        //md + od + term
//        String md = "531000009467677";
//        String od = "2088231760502253";
//        //od = URLDecoder.decode(od, "UTF-8");
//        String term = "SHW0843820";
//        String src = md + od + term;
//        System.out.println(checkMD5(src, cd, key));
    }

    public static boolean checkMD5(String src, String chk, String key) {
        byte[] srcb = null;
        try {
            srcb = (src + key).getBytes("UTF-8");
        } catch (Exception e) {
            return false;
        }
        String check = contMD5(srcb).substring(0, 16).toLowerCase();
        System.out.println(check);
        System.out.println(chk);
        if (check.equals(chk)) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * MD5加密
     */
    public static String contMD5(byte[] src) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(src);
            byte[] digest = md.digest();
            return byte2hex(digest);
        } catch (Exception e) {
            return "";
        }
    }

    public static String byte2hex(byte b[]) {
        if (b == null) {
            return "";
        }
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0xff);
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs.toUpperCase();
    }
}
